<template>
  <div>
    <div class="FatherOne">
      <el-input
        placeholder="请输入角色英文名"
        v-model="role.name"
        style="width: 300px; margin-right: 8px"
        size="small"
      >
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input
        v-model="role.nameZh"
        placeholder="请输入角色中文名"
        style="width: 300px; margin-right: 8px"
        size="small"
      ></el-input>
      <el-button type="primary" size="small" @click="doAddRole">添加角色</el-button>
    </div>
    <div class="shoufenqing">
      <el-collapse
        v-model="activeName"
        accordion
        style="width: 800px"
        @change="change"
      >
        <el-collapse-item
          :title="mana.nameZh"
          v-for="(mana, index) in manaArr"
          :key="index"
          :name="mana.id"
        >
          <!-- 卡片 -->
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button
                style="float: right; padding: 3px 0; color: red"
                type="text"
                icon="el-icon-delete-solid"
                @click="deleteClick(mana)"
              ></el-button>
              <div>
                <el-tree
                :key="index"
                  ref="tree"
                  show-checkbox
                  :data="allMenus"
                  :props="defaultProps"
                  :default-checked-keys="checkedKeyArr"
                  node-key="id"
                ></el-tree>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  margin-top: 10px;
                "
              >
                <el-button @click="quxiaoChange">取消修改</el-button>
                <el-button type="primary" @click="sureChange(mana.id, index)"
                  >确认修改</el-button
                >
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data() {
    return {
      role: {
        name: "",
        nameZh: "",
      },
      manaArr: [],
      allMenus: [],
      defaultProps: {
        label: "name",
        children: "children",
      },
      checkedKeyArr: [],
      activeName: -1,
    };
  },
  mounted() {
    this.initMana();
  },
  methods: {
    //删除按钮事件
    deleteClick(role){
 this.$confirm(
        "此操作将永久删除" + role.id + "," + "是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequest("/system/basic/permiss/role/", role.id).then((resp) => {
            if (resp) {
              this.initMana();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //quxiaoChange 取消修改事件
    quxiaoChange(){
 this.activeName = -1;
    },
        //添加角色
    doAddRole(){
if(this.role.name && this.role.nameZh){
this.postRequest('/system/basic/permiss/role',this.role).then(resp=>{
  if(resp){
this.initMana();
this.role.name='';
this.role.nameZh='';
  }else{
    this.$message.error('所有字段不能为空');
  }
})
}
    },
    change(rid) {
      if (rid) {
        this.initAllMenus();
        this.getMenueIdByRole(rid);
      }
    },
    // 根据角色ID查找菜单ID
    getMenueIdByRole(rid) {
      this.getRequest("/system/basic/permiss/mid/" + rid).then((resp) => {
        if (resp) {
          this.checkedKeyArr = resp;
        }
      });
    },
    initMana() {
      // 获取所有角色
      this.getRequest("/system/basic/permiss/").then((resp) => {
        if (resp) {
          this.manaArr = resp;
        }
      });
    },
    // 查询所有菜单
    initAllMenus() {
      this.getRequest("/system/basic/permiss/menus").then((resp) => {
        if (resp) {
          this.allMenus = resp;
        }
      });
    },
    //点击确认修改事件
    sureChange(rid, index) {
      let tree = this.$refs.tree[index];
      let selectedKeys = tree.getCheckedKeys(true);
      let url = "/system/basic/permiss/?rid=" + rid;
      selectedKeys.forEach((element) => {
        url += "&mids=" + element;
      });
      console.log(url);
      this.putRequest(url).then((resp) => {
        if (resp) {
          this.initMana();
          this.activeName = -1;
        }
      });
    },
  },
};
</script>

<style>
.FatherOne {
  display: flex;
}
.shoufenqing {
  margin-top: 20px;
}
</style>
